import openpyxl
import os

from openpyxl.utils import get_column_letter


def getCwd():
    """
    获取并改变当前工作目录
    """
    os.chdir('F://Download')
    print(os.getcwd())


def readExcel(filePath):
    """
    用openpyxl读取excel表格
    """
    # 用 chp12_excel 模块打开 Excel 文档
    wb = openpyxl.load_workbook(filePath)
    print(type(wb), "\n")

    # 获取xls中所有的sheet
    print(wb.sheetnames, "\n")

    # 获取xls中指定名称的sheet
    sheet = wb["Sheet1"]
    print("表格：{}，行数：{}，列数：{}\n".format(sheet.title, sheet.max_row, sheet.max_column))

    # 从表中取单元格
    # cell = sheet['B1']
    # cell = sheet.cell(1, 2)
    cell = sheet.cell(row=1, column=2)
    print(cell.value, cell.row, cell.column, cell.coordinate, "\n")

    rowCount = sheet.max_row
    columnCount = sheet.max_column
    for i in range(1, rowCount + 1):
        for j in range(1, columnCount + 1):
            print(sheet.cell(row=i, column=j).value, "\t", end="", flush=True)
        print()
    print()

    print(get_column_letter(1))


# 当我们用openpyxl_read.py执行时，内置变量__name__会是__main__，所以会进入if条件，调用指定函数
if __name__ == '__main__':
    # getCwd()
    readExcel("../files/openpyxl_read.xlsx")
